$(function () {
    function cha() {
        let sele=document.getElementsByName("userImg");
        let op="<option value='0'>---请选择---</option>"
        $.ajax({
            url: "/Role/All",
            type: "get",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (text) {
                for (let j=0;j<sele.length;j++){
                    sele[j].innerHTML=op;
                    for (var i=0;i<text.length;i++){
                        var option="<option value="+text[i].roleId+">"+text[i].roleName+"</option>"
                        sele[j].innerHTML+=option;
                    }
                }
            }
        })
    }
    cha()
    /*选中下面所有元素*/
    function checkAllChildren(obj,boo){
        var children=$(obj).next().next().find("input");

        if(children.length!=0){
            children.each(function(i,e){
                $(e).prop("checked",boo)//勾选
                checkAllChildren(e);
            })
        }
    }

    /*勾选所有父元素*/
    function checkAllParent(obj){
        if($(obj).parent().parent()[0]==$("#menu")[0])return false;
        var parent=$(obj).parent().parent().parent().find("input")//父元素
        console.log(parent[0])
        //判断上级是否还有父级元素
        if(parent[0]==obj){
            return false;
        }
        //判断兄弟元素是否全取消
        var boo=!$(obj).prop("checked");
        $(obj).parent().siblings().find("input").each(function(i,e){//遍历所有兄弟元素
            if($(e).prop("checked")){
                boo=false;
            }
        })
        //取消父元素选中，同时触发父元素取消事件
        $(parent[0]).prop("checked",!boo)
        checkAllParent(parent[0],!boo);
    }

    function menu() {
        let menu=$("#menu")
        $.ajax({
            url: "/menu/find",
            type: "get",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (text) {
                let one=""
                for(let i=0;i<text.length;i++){
                    if(text[i].parentId==0) {
                        one+= "\t\t\t\t<li><input type=\"checkbox\" value='" + text[i].menuId + "' class='frist'><span>" + text[i].menuName + "</span>\n" +
                            "\t\t\t\t<ul>\n"
                        for(let j=0;j<text.length;j++) {
                            if (text[j].menuGrade == '2' && text[i].menuId == text[j].parentId) {
                               one+="<li><input type=\"checkbox\" value='" + text[j].menuId + "' class='second'><span>" + text[j].menuName + "</span>\n" +
                                    "\t\t<ul>\n"
                                for(let y=0;y<text.length;y++){
                                    if(text[y].menuGrade == '3' && text[j].menuId == text[y].parentId){
                                        one+="<li><input type=\"checkbox\" value='" + text[y].menuId + "' class='third'><span>" + text[y].menuName + "</span>\n" +
                                            "\t\t<ul>\n"
                                        for(let q=0;q<text.length;q++){
                                            if(text[q].menuGrade == '4' && text[y].menuId == text[q].parentId){
                                                one+="<li><input type=\"checkbox\" value='" + text[q].menuId + "' class='fourth'><span>" + text[q].menuName + "</span>\n"
                                            }
                                            one+= "</li>"
                                        }
                                        one+= "</ul>\n"
                                    }
                                    one+= "</li>"
                                }
                                one+= "</ul>\n"
                            }
                            one+= "</li>"
                        }
                        one+= "</ul>\n"
                    }
                    one+= "</li>"
                }
                $("#menu").html(one)
                $(".frist,.second,.third,.fourth").click(function(){
                    var boo=$(this).prop("checked");//当前元素是否选中
                    var t=this;

                    checkAllChildren(this,boo)//选中下面所有子元素的方法
                    setTimeout(function(){
                        checkAllParent(t)
                    },50)
                })
            }
        })
    }
    menu()
})
$("select").change(function () {
    $("input").prop("checked",false)
    let rname=$("#role").val()
    $.ajax({
        url: "/menu/"+rname,
        type: "get",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (text) {
            for(let i=0;i<text.length;i++){
                $("input[value='"+text[i].menuId+"']").prop("checked",true)
            }
        }
    })
})
function menuRole() {
    let rname=$("#role").val()
    let arr=[];
    $("input:checkbox:checked").each(function(i){
        arr.push($(this).val());
    });
    if(rname!=0){
        if (confirm("确定修改该角色的菜单吗？")){
            $.ajax({
                url: "/menu/"+rname,
                type: "delete",
                success: function (text) {
                    $.ajax({
                        url: "/menu/add/",
                        type: "post",
                        data:{
                            roleId:rname,
                            arr:arr.toString()
                        },
                        success: function (text) {
                            parent.window.location.reload();
                        }
                    })
                }
            })
        }

    }else{
        alert("请选择你需要授予菜单的角色")
    }

}


